package com.zhijian.medical.controller;

import com.alibaba.fastjson.JSON;
import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.config.Constants;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.vo.request.ChangePwdVo;
import com.zhijian.medical.entity.vo.request.LoginVo;
import com.zhijian.medical.entity.vo.request.RegisterTrainByAssessmentVo;
import com.zhijian.medical.entity.vo.response.AuthVo;
import com.zhijian.medical.service.LoginService;
import com.zhijian.medical.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 登录相关
 *
 * @author HCG
 * @version 1.0
 * @date 2022/1/17
 */
@Slf4j
@Controller
@Api(tags = "登录相关")
public class LoginController {

    @Autowired
    private LoginService service;

    @Autowired
    private PersonService personService;

    /**
     * 登录
     *
     * @param vo
     */
    @Response
    @PostMapping("/login")
    @ApiOperation("登录")
    public AuthVo login(@RequestBody LoginVo vo) {
        log.info("{}| login ", JSON.toJSONString(vo));
        return service.login(vo);
    }

    /**
     * 用户登出
     *
     * @param token
     * @return
     */
    @Response
    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public boolean logout(@RequestHeader(value = Constants.MEDICAL_TOKEN) String token) {
        log.info("{}| logout info", token);
        return service.logout(token);
    }

    /**
     * 修改密码
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/change/password")
    @ApiOperation("修改密码")
    public boolean updatePassWord(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody ChangePwdVo param) {
        log.info("{}| update info, {}", param);
        service.changePassword(personDto.getId(), param.getOldPassword(), param.getNewPassword());
        return true;
    }

    /**
     * 基于一个考核注册专项培训人员
     *
     * @param registerTrainByAssessmentVo
     * @return
     */
    @Response
    @PostMapping("/register/assessment")
    @ApiOperation("基于一个考核注册专项培训人员")
    public String registerTrainByAssessment(@RequestBody RegisterTrainByAssessmentVo registerTrainByAssessmentVo) {
        log.info("the enroll info is {}", registerTrainByAssessmentVo);
        return personService.registerTrainByAssessment(registerTrainByAssessmentVo);
    }

}
